{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a name=\"top\"></a><img src=\"source/SpinalHDL.png\" alt=\"SpinalHDL based on Scala\" style=\"width:320px;\" />"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Before running Spinal HDL code, be sure to load SpinalHDL Libraries  \n",
    "**Note** : This may be a little slow when the first time load, please wait a moment to download Lib from remote.)   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "val path = System.getProperty(\"user.dir\") + \"/source/load-spinal.sc\"\n",
    "interp.load.module(ammonite.ops.Path(java.nio.file.FileSystems.getDefault().getPath(path)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## LattencyAnalysis\n",
    "\n",
    "```scala\n",
    "LatencyAnalysis(dut.io.input, dut.io.output)\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class T1 extends Component{\n",
    "   val a,b  = in UInt(2 bits)\n",
    "   val c = RegNext(a)\n",
    "   val d = RegNext(c*b)\n",
    "   val e = RegNext(d)\n",
    "   val f = e + b\n",
    "    \n",
    "println(s\"latency ${LatencyAnalysis(a,c)}\")\n",
    "println(s\"latency ${LatencyAnalysis(a,d)}\")\n",
    "println(s\"latency ${LatencyAnalysis(a,e)}\")\n",
    "println(s\"latency ${LatencyAnalysis(a,f)}\")    \n",
    "}\n",
    "showRtl(new T1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## OneHot/OneHotMask\n",
    "\n",
    "- `OHToUInt(bits)`\n",
    "- `OHMasking.first(bits)` OneHotMask From Lowerest bit\n",
    "- `OHMasking.last(bits)` OneHotMask From Highest bit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class OneHot(w: Int) extends Component{\n",
    "  val bits   = in Bits(w bits)\n",
    "  val int0   = out(OHToUInt(bits))\n",
    "  val int2   = out(OHToUInt(OHMasking.first(bits)))\n",
    "  val int3   = out(OHToUInt(OHMasking.last(bits)))\n",
    "  val int4   = ~int3\n",
    "  val int7   = U(w-1) - int3\n",
    "}"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Scala",
   "language": "scala",
   "name": "scala"
  },
  "language_info": {
   "codemirror_mode": "text/x-scala",
   "file_extension": ".scala",
   "mimetype": "text/x-scala",
   "name": "scala",
   "nbconvert_exporter": "script",
   "version": "2.12.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
